package OptimalAlgorithm.Stack;

import java.util.Stack;

public class RemoveDuplicates {
    public String removeDuplicates(String s) {
        char[] ch = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        stack.push(ch[0]);
        for (int i = 1; i < s.length(); i++) {
            if(!stack.isEmpty()&&stack.peek() == ch[i]){
                stack.pop();
            }else {
                stack.push(ch[i]);
            }
        }
        StringBuilder ret = new StringBuilder();
        while (!stack.isEmpty()){
            ret.append(stack.pop());
        }
        return ret.reverse().toString();
    }
}
